<?php

use yii\db\ActiveRecordInterface;
use yii\helpers\Inflector;
use yii\helpers\StringHelper;


/* @var $this yii\web\View */
/* @var $generator backend\modules\gii\generators\tree\Generator */

$controllerClass = StringHelper::basename($generator->controllerClass);
$modelClass = StringHelper::basename($generator->modelClass);
$searchModelClass = StringHelper::basename($generator->searchModelClass);
if ($modelClass === $searchModelClass)
{
    $searchModelAlias = $searchModelClass . 'Search';
}

/* @var $class ActiveRecordInterface */
$class = $generator->modelClass;
$pks = $class::primaryKey();
$urlParams = $generator->generateUrlParams();
$actionParams = $generator->generateActionParams();
$actionParamComments = $generator->generateActionParamComments();

$title = Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass)));

echo "<?php\n";

?>

namespace <?= StringHelper::dirname(ltrim($generator->controllerClass, '\\')) ?>;

use <?php echo ltrim($generator->baseControllerClass, '\\') ?>;
use <?php echo ltrim($generator->treeModelClass, '\\') ?>;
use common\widgets\tree\TreeView;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;

/**
* <?= $controllerClass ?> implements the CRUD actions for <?= $modelClass ?> model.
*/
class <?= $controllerClass ?> extends <?php echo StringHelper::basename($generator->baseControllerClass) . "\n" ?>
{

    /**
     * {@inheritdoc}
     */
    public function init()
    {
        parent::init();
        $this->defaultPostData = ArrayHelper::merge($this->defaultPostData, [
            'modelClass' => '<?php echo ltrim($generator->treeModelClass, '\\') ?>',
        ]);
    }

    /**
     * 首页
     *
     * @return string
     */
    public function actionIndex()
    {
        return $this->render(TreeView::DEFAULT_FORM_VIEW, [
            'title' => <?php echo $generator->generateString($title) ?>,
            'treeConfig' => [
                'query' => <?php echo StringHelper::basename($generator->treeModelClass) ?>::find()->addOrderBy('root, lft'),
                'headingOptions' => ['label' => <?php echo $generator->generateString($title) ?>],
                'fontAwesome' => true,
                'isAdmin' => true,
                'displayValue' => 1,
                'nodeActions' => [
                    'save' => Url::to(['save']),
                    'manage' => Url::to(['manage']),
                    'remove' => Url::to(['remove']),
                    'move' => Url::to(['move']),
                ],
                'softDelete' => true,
                'cacheSettings' => [
                    'enableCache' => true,
                ]
            ],
        ]);
    }
}
